package com.ymh.git.thread.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 新启一个线程的3种方式
 * Created by aivin on 2018/10/28.
 */
public class NewThread {

    /**
     * 继承Thread类
     */
    public static class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            System.out.println(Thread.currentThread().getName() + " am extends Thread");
        }
    }

    /**
     * 实现Runnable接口
     */
    public static class MyRunnable implements Runnable {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "am implements Runnable");
        }
    }

    /**
     * 实现Callable接口
     */
    public static class MyCallable implements Callable<String> {


        @Override
        public String call() throws Exception {
            System.out.println(Thread.currentThread().getName() + "am implements Callable");
            return "Call Result";
        }
    }


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyThread myThread = new MyThread();
        myThread.start();

        MyRunnable myRunnable = new MyRunnable();
        Thread thread = new Thread(myRunnable);
        thread.start();

        MyCallable myCallable = new MyCallable();
        FutureTask<String> futureTask = new FutureTask<String>(myCallable); // 包装
        new Thread(futureTask).start();
        System.out.println("Get MyCallable Result:" + futureTask.get()); // 拿结果

    }
}
